<template>
  <!-- 登录页面大容器 -->
  <div class="login_containe">
    <!-- 登陆区域 -->
    <div class="login_box">
      <!-- LOGO区 -->
      <div class="avatar_box">
        <img src="../assets/img/logo.png" alt="" />
      </div>
      <!-- 登陆表单 -->
      <el-form :model="loginForm" ref="loginForm" :rules="rules" label-width="100px" class="login_form">
        <el-form-item prop="username" label="用户名">
          <el-input v-model="loginForm.username" placeholder="账号"></el-input>
        </el-form-item>

        <el-form-item prop="password" label="密码">
          <el-input v-model="loginForm.password" type="password" autocomplete="off" placeholder="密码"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="submitLoginForm('loginForm')">登录</el-button>
          <el-button @click="resetForm('loginForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  inject: ['reload'],
  data() {
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入账户名', trigger: 'blur' },
          { min: 4, max: 12, message: '长度在 4 到 12 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 4, max: 12, message: '长度在 4 到 12 个字符', trigger: 'blur' }
        ],
      }
    };
  },
  methods: {
    async submitLoginForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          const { data: result } = await this.$http.post("user/login", this.loginForm);
          //登录成功
          if (result.success) {
            this.$message.success("登录成功");
            this.$store.commit("setUserInfo", result.data);
            // this.$global.startLoading()
            // await this.$global.getMenu()
            this.reload();
            // this.$global.endLoading()
            window.sessionStorage.setItem('token', result.token);
            // console.log(window.sessionStorage.getItem("token"))
            this.$router.push("/welcome");
          } else {//登录失败
            this.$message.error(result.errorMsg);
          }
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>

<style>
.login_containe {
  background-color: #009688;
  width: 100%;
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #efef;
  border-radius: 3px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
}

.login_form {
  position: absolute;
  bottom: 0px;
  width: 100%;
  padding: 0px 20px;
  box-sizing: border-box;
}

.avatar_box {
  height: 130px;
  width: 130px;
  border: 1px solid #eee;
  border-radius: 50%;
  padding: 10px;
  box-shadow: 0 0 10px #ddd;
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  background-color: #fff;
}

.avatar_box img {
  height: 100%;
  width: 100%;
  border-radius: 50%;
  background-color: #eee;
}
</style>